package com.work.work.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.work.common.R;
import com.work.work.entity.Notification;
import com.work.work.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "通知管理")
@RestController
@RequestMapping("/notification")
public class NotificationController {
    @Autowired
    private NotificationService notificationService;

    @Operation(summary = "发送通知")
    @PostMapping("/send")
    public R<Notification> send(@RequestBody Notification notification) {
        if (notification.getSenderId() == null) {
            return R.fail("发送者ID不能为空");
        }
        if (notification.getReceiverId() == null) {
            return R.fail("接收者ID不能为空");
        }
        if (notification.getTitle() == null || notification.getTitle().trim().isEmpty()) {
            return R.fail("通知标题不能为空");
        }

        notification.setSendTime(LocalDateTime.now());
        notification.setIsRead(0);

        return notificationService.save(notification) ?
            R.ok(notification, "项目创建成功") : R.fail("发送通知失败");
    }

    @Operation(summary = "获取收到的通知")
    @GetMapping("/received/{receiverId}")
    public R<Page<Notification>> getReceivedNotifications(
            @PathVariable Integer receiverId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return R.ok(notificationService.getReceivedNotifications(receiverId, current, size), "项目创建成功");
    }

    @Operation(summary = "获取发送的通知")
    @GetMapping("/sent/{senderId}")
    public R<Page<Notification>> getSentNotifications(
            @PathVariable Integer senderId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return R.ok(notificationService.getSentNotifications(senderId, current, size), "项目创建成功");
    }

    @Operation(summary = "获取未读通知")
    @GetMapping("/unread/{receiverId}")
    public R<List<Notification>> getUnreadNotifications(@PathVariable Integer receiverId) {
        return R.ok(notificationService.getUnreadNotifications(receiverId), "项目创建成功");
    }

    @Operation(summary = "标记通知为已读")
    @PutMapping("/read/{id}")
    public R<Boolean> markAsRead(@PathVariable Integer id) {
        return notificationService.markAsRead(id) ?
            R.ok(true, "项目创建成功") : R.fail("标记已读失败");
    }

    @Operation(summary = "标记所有通知为已读")
    @PutMapping("/read/all/{receiverId}")
    public R<Boolean> markAllAsRead(@PathVariable Integer receiverId) {
        return notificationService.markAllAsRead(receiverId) ?
            R.ok(true, "项目创建成功") : R.fail("标记全部已读失败");
    }

    @Operation(summary = "删除通知")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        return notificationService.removeById(id) ?
            R.ok(true, "项目创建成功") : R.fail("删除通知失败");
    }
}